<template>
  <div>
    <el-button type="primary" @click="openAddDialog">添加门店</el-button>
    <el-table :data="stores" style="width: 100%">
      <el-table-column prop="StoreName" label="门店名称" />
      <el-table-column prop="Address" label="地址" /> <!-- 使用 Address 替换 Location -->
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button size="small" @click="editStore(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteStore(row.StoreID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="门店信息">
      <el-form :model="form">
        <el-form-item label="门店名称">
          <el-input v-model="form.StoreName" />
        </el-form-item>
        <el-form-item label="城市">
          <el-input v-model="form.City" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.Address" />
        </el-form-item>
        <el-form-item label="营业时间">
          <el-input v-model="form.details.OpenHours" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.details.ContactPhone" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveStore">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import http from '@/utils/http'
import { ElMessage } from 'element-plus'

const stores = ref([]) // 存储表格数据
const dialogVisible = ref(false)
const form = ref({
  StoreName: '',
  City: '',
  Address: '',
  Latitude: '',
  Longitude: '',
  details: {
    OpenHours: '',
    ContactPhone: '',
    Features: '',
    Email: ''
  }
})
const editingId = ref(null)

// 获取门店列表
const fetchStores = async () => {
  try {
    const res = await http.get('stores/')
    stores.value = res.data || [] // 直接赋值给 stores
  } catch (error) {
    console.error('获取门店列表失败:', error)
    ElMessage.error('获取门店列表失败')
  }
}

// 打开新增对话框
const openAddDialog = () => {
  form.value = {
    StoreName: '',
    City: '',
    Address: '',
    Latitude: '',
    Longitude: '',
    details: {
      OpenHours: '',
      ContactPhone: '',
      Features: '',
      Email: ''
    }
  }
  editingId.value = null
  dialogVisible.value = true
}

// 编辑门店
const editStore = (store) => {
  form.value = {
    ...store,
    details: store.details || {}
  }
  editingId.value = store.StoreID
  dialogVisible.value = true
}

// 保存门店
const saveStore = async () => {
  if (!form.value.StoreName || !form.value.City) {
    ElMessage.warning('门店名称和城市为必填项')
    return
  }

  try {
    const payload = { ...form.value }

    if (editingId.value) {
      await http.put(`stores/${editingId.value}/`, payload)
      ElMessage.success('更新成功')
    } else {
      await http.post('stores/', payload)
      ElMessage.success('新增成功')
    }

    dialogVisible.value = false
    fetchStores()
  } catch (error) {
    console.error('保存门店失败:', error)
    const errorMessage = error.response?.data?.message ||
                         error.response?.statusText ||
                         '保存门店信息失败'
    ElMessage.error(errorMessage)
  }
}

// 删除门店
const deleteStore = async (id) => {
  try {
    await http.delete(`stores/${id}/`)
    fetchStores()
  } catch (error) {
    ElMessage.error('删除失败')
  }
}

onMounted(fetchStores)
</script>